package com.itheima.springbootwebtlias.controller;

import com.itheima.springbootwebtlias.pojo.Emp;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.service.EmpService;
import com.itheima.springbootwebtlias.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        //接收请求参数
       //json格式： @RequestBody + 实体对象
        log.info("登录，请求参数：{}", emp);

        //调用业务层Service功能方法
        Emp resultEmp = empService.checkLogin(emp);


//封装返回结果
        Map map = new HashMap();
        map.put("id",resultEmp.getId());
        map.put("username",resultEmp.getUsername());
        map.put("name",resultEmp.getName());

        //4.登陆成功
        String jwtToken = JwtUtils.generateJwt(map);
        map.put("token",jwtToken);
        log.info("登录成功，返回结果：{}", jwtToken);

        return Result.success(map);



    }
}
